//
//  PushJobNeedsViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/10.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "PushJobNeedsViewController.h"
#import "PushJobTableViewCell.h"


@interface PushJobNeedsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * showStringArray;
//textFiled 数据
@property (nonatomic, copy)NSString * nameStr;
@property (nonatomic, copy)NSString * industryStr;
@property (nonatomic, copy)NSString * positionStr;
@property (nonatomic, copy)NSString * moneyStr;
@property (nonatomic, copy)NSString * educationStr;
@property (nonatomic, copy)NSString * exploreStr;
@property (nonatomic, copy)NSString * companyStr;
@property (nonatomic, copy)NSString * adressStr;
@property (nonatomic, copy)NSString * telStr;

@end

@implementation PushJobNeedsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = WHITE_COLOR;
    
    self.showStringArray = @[@"职位名称",@"行业",@"职位",@"薪资",@"学历要求",@"岗位描述",@"公司名称",@"工作地点",@"联系电话"];
    
    [self setupNav];
    [self setupUI];
    
}


- (void)setupNav{
    
    self.title = @"发布职位";
    
    UIButton * backButton = [[UIButton alloc] init];
    
    backButton.frame = CGRectMake(0, 0, 12 * SCREENH6, 20 * SCREENH6);
    [backButton setImage:[UIImage imageNamed:@"1返回"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backToView) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
}

- (void)backToView{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 460 * SCREENH6) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 50 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled  = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[PushJobTableViewCell class] forCellReuseIdentifier:@"PushJobListCell"];
//    [self.tableView registerClass:[HomeHeadView class] forHeaderFooterViewReuseIdentifier:@"HomeView"];
    
    [self.view addSubview:self.tableView];
    
    UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50 * SCREENH6, 480 * SCREENH6, 275 * SCREENH6, 55 * SCREENH6)];
    
    [button setTitle:@"确认发布" forState:UIControlStateNormal];
    [button setBackgroundColor:MAINCOLOR];
    button.titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    [button addTarget:self action:@selector(cilckSureButon) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:button];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.showStringArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PushJobTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PushJobListCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.showStringArray[indexPath.row];
    
//    __weak typeof(self) weakSelf = self;
    
    if (indexPath.row == 0) {
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.nameStr = text;
        };
    } else if (indexPath.row == 1) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.industryStr = text;
        };
    } else if (indexPath.row == 2) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.positionStr = text;
        };
    }else if (indexPath.row == 3) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.moneyStr = text;
        };
    }else if (indexPath.row == 4) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.educationStr = text;
        };
    }else if (indexPath.row == 5) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.exploreStr = text;
        };
    }else if (indexPath.row == 6) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.companyStr = text;
        };
    }else if (indexPath.row == 7) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.adressStr = text;
        };
    }else if (indexPath.row == 8) {
        // 同上，请自行脑补
        cell.block = ^(NSString * text) {
            // 更新数据源
            self.telStr = text;
        };
    }
    
    
    return cell;
}

- (void)cilckSureButon{
    
    SHOWHUD;
    
    NSString * token = DEF_GET_OBJECT(@"token");
    
    NSDictionary * dict = @{@"token":token,@"trade":self.industryStr,@"position":self.positionStr,@"salary":self.moneyStr,@"education":self.educationStr,@"describe":self.exploreStr,@"company":self.companyStr,@"address":self.adressStr,@"mobile":self.telStr};
    
    [[NetworkTool sharedTools] POST:@"/index/Liuheonline/jobAdd" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        HIDDENHUD;
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:@"恭喜你，发布成功，快去看看吧！"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      [self.navigationController popViewControllerAnimated:YES];
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                           message:responseObject[@"msg"]
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction * action) {
                                                                      
                                                                      
                                                                  }];
            
            [alert addAction:defaultAction];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        HIDDENHUD;
        
        NSLog(@"error==%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
